package com.longrise.android.byjk.plugins.tabthird;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/14.
 * 我的订单adapter
 */

public class MineOrderAdapter extends BaseQuickAdapter<EntityBean, BaseViewHolder>{


    private OnItemClickListener mListener;
    private List<EntityBean> mListBeans = new ArrayList<>();
    private String orderState = "";

    public MineOrderAdapter() {
        super(R.layout.item_mine_order);
    }

    public void refreshData(EntityBean[] beans) {
        mListBeans.clear();
        for (EntityBean bean : beans) {
            mListBeans.add(bean);
        }
        setNewData(mListBeans);
    }

    @Override
    protected void convert(BaseViewHolder helper, final EntityBean item) {
        final View convertView = helper.getConvertView();
        TextView item_mine_order_num = (TextView) convertView.findViewById(R.id.item_mine_order_num);
        TextView item_mine_order_state = (TextView) convertView.findViewById(R.id.item_mine_order_state);
        final TextView item_mine_order_point = (TextView) convertView.findViewById(R.id.item_mine_order_point);
        TextView item_mine_order_time = (TextView) convertView.findViewById(R.id.item_mine_order_time);
        TextView item_mine_order_type = (TextView) convertView.findViewById(R.id.item_mine_order_type);
        TextView item_mine_order_price = (TextView) convertView.findViewById(R.id.item_mine_order_price);
        ImageView item_mine_order_img = (ImageView) convertView.findViewById(R.id.item_mine_order_img);

        final int trade_state = item.getInt("trade_state");
        final int buytype  = item.getInt("buytype");
        final String orderid = item.getString("out_trade_no");

        item_mine_order_num.setText("订单编号: "+orderid);
        item_mine_order_state.setText(getOrderState(trade_state));
        item_mine_order_point.setText(item.getString("subject"));
        item_mine_order_time.setText(item.getString("createtime"));
        item_mine_order_price.setText("¥"+item.getString("total_fee"));
        if(0 == trade_state){
            item_mine_order_state.setTextColor(Color.parseColor("#FFFBA140"));
        }else {
            item_mine_order_state.setTextColor(Color.parseColor("#FF999999"));
        }

        //HRA预约
        if(4 == buytype){
            item_mine_order_img.setImageResource((R.drawable.bg_my_ic_my_order_hra));
            item_mine_order_type.setText("HRA预约");
        }else {
            item_mine_order_img.setImageResource((R.drawable.by_ic_my_order_class));
            item_mine_order_type.setText("培训");
        }


        item_mine_order_point.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = item_mine_order_point.getLineCount();
                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                if (lineCount >= 2) {
                    layoutParams.height = AppUtil.dip2px(200);
                } else {
                    layoutParams.height = AppUtil.dip2px(169);
                }
                convertView.setLayoutParams(layoutParams);
            }
        });

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClick(orderid,trade_state,buytype);
                }
            }
        });
    }

    public String getOrderState(int state){
        if(0 == state){
            orderState = "待付款";
        }else if(1 == state){
            orderState = "已付款";
        }else if(-1 == state){
            orderState = "已取消";
        }
        return orderState;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnItemClickListener {
        void onClick(String orderid,int orderState,int buyType);
    }

}
